package com.yilemall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilemall.common.utils.PageUtils;
import com.yilemall.common.utils.Query;


import com.yilemall.product.dao.CategoryDao;
import com.yilemall.product.entity.CategoryEntity;
import com.yilemall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有商品分类列表
     * @return 商品分类列表
     */
    @Override
    public List<CategoryEntity> listWistTree() {
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
        //一级所有菜单
        List<CategoryEntity> level01Menue = categoryEntityList.stream().filter((categoryEntity -> {
            return categoryEntity.getParentCid() == 0;
        })).map((menu)->{
            menu.setChildren(getChildren(menu,categoryEntityList));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return level01Menue;
    }

    /**
     * 删除未被引用的菜单
     * @param asList 待删除的id
     */
    @Override
    public void removeMenuByIds(List<Long> asList) {
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 检测分类是否超过两级
     * @param parentCid 父id
     * @return
     */
    @Override
    public boolean checkCategory(Long parentCid) {
        if(parentCid == 0) return true;
        CategoryEntity categoryEntity =  this.getOne(Wrappers.<CategoryEntity>lambdaQuery()
                .eq(CategoryEntity::getCatId,parentCid));
        if(categoryEntity.getParentCid() > 0) {
            return false;
        }

        return true;
    }

    @Override
    public Long[] findCategoryPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPaths = findParentPaths(catelogId, paths);
        Collections.reverse(parentPaths);
        return (Long[]) parentPaths.toArray(new Long[parentPaths.size()]);
    }

    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())){
            categoryDao.updateDetail(category.getCatId(),category.getName());
        }
    }

    public List<Long> findParentPaths(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if(categoryEntity.getParentCid()!=0){
            findParentPaths(categoryEntity.getParentCid(),paths);
        }
        return paths;
    }

    /**
     *  递归查找菜单的子菜单
     * @param root 当前菜单
     * @param all  所有菜单
     * @return
     */
    public List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
            //找到子菜单
        }).map(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
            //菜单排序
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}